package com.ruoyi.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.AppPage;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.gwc.domain.*;
import com.ruoyi.gwc.service.*;
import com.ruoyi.system.domain.request.PageParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 费用管理模块
 */
@RestController
@RequestMapping("/app/repairManagement")
@Slf4j
public class AppRepairManagement extends BaseController {

    @Autowired
    private ITRepairManagementService itRepairManagementService;
    @Autowired
    private ITApprovalRepairService itApprovalRepairService;


    @ApiOperation("查看所有维修申请单")
    @GetMapping("/repairApplyList")
    public AjaxResult repairApplyList(@RequestParam(value = "orderStatus", required = false ) Long orderStatus,
                                      @RequestParam(value = "companyId", required = false ) Long companyId,
                                      @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        TRepairManagement tRepairManagement = new TRepairManagement();
        LoginUser loginUser = getLoginUser();
        if(loginUser.getUser().getUserType() == 2) {//用户类型（0系统用户，1移动端用户，2驾驶员，3管理员）
            tRepairManagement.setDriverId(loginUser.getUser().getUserId());
        }
        tRepairManagement.setOrderStatus(orderStatus);

        if (companyId == null){
            companyId = loginUser.getDeptId();
        }
        tRepairManagement.setCompanyId(companyId);
        List<TRepairManagement> list = itRepairManagementService.selectTRepairManagementList(tRepairManagement);
        PageInfo<TRepairManagement> page = new PageInfo<>(list);
        return  success(new AppPage<>(page));
    }

    @ApiOperation("查看某个指定维修申请单")
    @GetMapping("/getRepairApplyByRepairId")
    public AjaxResult getRepairApplyByRepairId(@RequestParam Long repairId) {
        TRepairManagement tRepairManagement = itRepairManagementService.selectTRepairManagementByRepairId(repairId);
        return success(tRepairManagement);
    }

    @ApiOperation("保存维修申请单")
    @PostMapping("/saveRepairApply")
    public AjaxResult saveRepairApply(@RequestBody TRepairManagement param) {
        LoginUser loginUser = getLoginUser();
        Long driverId = loginUser.getUser().getUserId();
        param.setDriverId(driverId);
        param.setRepairId(SnowFlakeIDGenerator.getInstance().nextId());
        itRepairManagementService.save(param);
        return success();
    }

    @ApiOperation("修改维修申请单")
    @PostMapping("/updateRepairApply")
    public AjaxResult updateRepairApply(@RequestBody TRepairManagement param) {
        itRepairManagementService.saveOrUpdate(param);
        return success();
    }

    @ApiOperation("查看申请维修审批单")
    @GetMapping("/getApprovalRepairByRepairId")
    public AjaxResult getApprovalRepairByRepairId(@RequestParam Long repairId) {
        TApprovalRepair tApprovalRepair = new  TApprovalRepair();
        tApprovalRepair.setRepairId(repairId);
        List<TApprovalRepair> list = itApprovalRepairService.selectTApprovalRepairList(tApprovalRepair);
        return success(list);
    }

    @ApiOperation("添加申请维修审批单")
    @PostMapping("/saveRepairApproval")
    public AjaxResult saveRepairApproval(@RequestBody TApprovalRepair param) {
       if (itApprovalRepairService.insertTApprovalRepair(param) > 0){
           //审批通过之后更新状态
           if(param.getApprovealStatus() == 0){
               TRepairManagement tRepairManagement =  new TRepairManagement();
               tRepairManagement.setRepairId(param.getRepairId());
               tRepairManagement.setOrderStatus(1L);//订单状态（0:已申请，1：已审批，2：已送修，3：维修完成，4：撤销订单）
               itRepairManagementService.saveOrUpdate(tRepairManagement);
           }

       }
        return success();
    }

    @ApiOperation("维修凭证上传图片")
    @PostMapping("/uploadRepair")
    public AjaxResult uploadRepair(@RequestBody MultipartFile[] file) {
        return success(uploadFile(file, "repair"));
    }


}
